/**
 * 
 */
package vn.edu.hungvuongaptech.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.model.RoleModel;
import vn.edu.hungvuongaptech.util.DataUtil;

/**
 * @author ThanhHQ
 *
 */
public class RoleDAO {

	public static RoleModel getRoleModelById(int roleId) {
		try {
			PreparedStatement statement = DataUtil.getConnection()
					.prepareStatement(
							Constant.SQL_RES
									.getString("timesheet.sql.getRoleById"));
			statement.setInt(1, roleId);
			ResultSet rs = statement.executeQuery();
			RoleModel roleModel = new RoleModel();
			while (rs.next()) {
				roleModel.setRoleId(rs.getInt("RoleID"));
				roleModel.setRoleName(rs.getString("RoleName"));
				roleModel.setRoleDescription(rs.getString("RoleDescription"));
			}
			return roleModel;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<RoleModel> getAllRoles() {
		ArrayList<RoleModel> roleModels = new ArrayList<RoleModel>();
		try {
			PreparedStatement statement = DataUtil.getConnection()
					.prepareStatement(
							Constant.SQL_RES.getString("timesheet.sql.getAllRole"));
			ResultSet rs = statement.executeQuery();
			RoleModel roleModel = null;
			while (rs.next()) {
				roleModel = new RoleModel();
				roleModel.setRoleId(rs.getInt("RoleID"));
				roleModel.setRoleName(rs.getString("RoleName"));
				roleModel.setRoleDescription(rs.getString("RoleDescription"));
				roleModels.add(roleModel);
			}
			return roleModels;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
